package com.macro.mall.service.impl;

import com.macro.mall.dto.TenantInfoDto;
import com.macro.mall.mapper.PlatFormMapper;
import com.macro.mall.mapper.TenantMapper;
import com.macro.mall.model.TenantEntity;
import com.macro.mall.service.TenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 *  商家管理后台实现
 *
 * @author zhang
 * @date 2021/06/19
 */
@Service
public class TenantServiceImpl implements TenantService {

    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private PlatFormMapper platFormMapper;

    @Override
    public TenantEntity findTenantInfo(String userName) {
        TenantEntity tenantInfo = tenantMapper.findTenantInfo(userName);
        if(tenantInfo == null){
            return new TenantEntity();
        }
        Long id = tenantInfo.getId();
        List<Long> desiginAreas = platFormMapper.findDesiginAreaByTenId(id);
        tenantInfo.setAreaIds(desiginAreas);
        return tenantInfo;
    }

    @Override
    public TenantEntity findTenantInfoById(Long tenantId) {
        TenantEntity tenantInfo = tenantMapper.findTenantInfoById(tenantId);
        List<Long> desiginAreas = platFormMapper.findDesiginAreaByTenId(tenantId);
        tenantInfo.setAreaIds(desiginAreas);
        if(tenantInfo == null){
            return new TenantEntity();
        }
        return tenantInfo;
    }

    @Override
    public void updateTenantInfo(TenantInfoDto tenantInfoDto) {
        tenantMapper.updateTenantInfo(tenantInfoDto);
        List<Long> areaIds = tenantInfoDto.getAreaIds();
        if(!CollectionUtils.isEmpty(areaIds)){
            Long id = tenantInfoDto.getId();
            // 删除商家管理小区
            tenantMapper.delectTenantArea(id);
            // 现在商家关联小区
            tenantMapper.addTenantArea(areaIds, id);
        }
    }
}
